package app.android.alampang.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import app.android.alampang.R;
import app.android.alampang.loader.PlaceInfoLoader;
import app.android.alampang.model.Place;
import app.android.alampang.ui.adapter.PlaceListAdapter;
import app.android.alampang.ui.core.BaseActivity;
import app.android.alampang.ui.custom.FloatingActionButton;
import app.android.alampang.utility.NavigationService;

public class MenuPlaceActivity extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener{

    private ListView listPlace;
    private PlaceListAdapter listPlaceAdapter;

    private FloatingActionButton btnQRScanner;

    @Override
    public void onCreate(Bundle savedInstanceState, Bundle param) {
        setContentView(R.layout.activity_menu_place);
    }

    @Override
    public void initialize() {
        btnQRScanner = (FloatingActionButton) findViewById(R.id.btn_scanner);
        btnQRScanner.setOnClickListener(this);

        listPlace = (ListView) this.findViewById(R.id.list_place);
        listPlaceAdapter = new PlaceListAdapter(this);

        final ArrayList<Place> places = PlaceInfoLoader.getAllPlaces();

        for (Place p : places){
            listPlaceAdapter.add(p);
        }

        listPlace.setAdapter(listPlaceAdapter);

        listPlace.setOnItemClickListener(this);
    }

    @Override
    public void onLock() {

    }

    @Override
    public void onUnLock() {

    }

    @Override
    public void onReset() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle param = new Bundle();
        //Toast.makeText(MenuPlaceActivity.this, listPlaceAdapter.get(position).toJSONObject().toString(),Toast.LENGTH_LONG).show();
        param.putString(Place.PARSING_NAME, listPlaceAdapter.get(position).toJSONObject().toString());

        NavigationService.getInstance().navigate(this, PlaceDetailActivity.class,
                param, false);
    }

    @Override
    public void onClick(View v) {
        onClickScanQRCode();
    }

    private void onClickScanQRCode()
    {
        try {

            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for bar codes

            startActivityForResult(intent, 0);

        } catch (Exception e) {

            Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
            Intent marketIntent = new Intent(Intent.ACTION_VIEW,marketUri);
            startActivity(marketIntent);

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {

            if (resultCode == RESULT_OK) {
                String contents = data.getStringExtra("SCAN_RESULT");

                Place place = PlaceInfoLoader.getPlaceByName(contents);
                if (place == null)
                {
                    Toast.makeText(this, "QR code ไม่ถูกต้อง", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Bundle param = new Bundle();
                    param.putString(Place.PARSING_NAME, place.toJSONObject().toString());

                    NavigationService.getInstance()
                            .navigate(this, PlaceDetailActivity.class, param, false);
                }

            }
            if(resultCode == RESULT_CANCELED){
                //handle cancel
            }
        }
    }
}
